/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.utils.CacheUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.dao.StoreProductDao;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.base.service.CustomerService;
import com.chlitina.store.modules.base.service.StoreProductService;
import com.chlitina.store.modules.cash.model.Member;
import com.chlitina.store.modules.cash.model.MemberShoppingCart;
import com.chlitina.store.modules.cash.model.SolutionStoreProductCart;
import com.chlitina.store.modules.cash.service.MemberService;
import com.chlitina.store.modules.employee.service.EmployeeService;
import com.chlitina.store.modules.expert.entity.CustomerExam;
import com.chlitina.store.modules.expert.entity.CustomerSolution;
import com.chlitina.store.modules.expert.entity.CustomerSolutionAndProduct;
import com.chlitina.store.modules.expert.service.CustomerSolutionAndProductService;
import com.chlitina.store.modules.expert.service.CustomerSolutionService;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 客户采纳产品Controller
 * @author lvbo
 * @version 2013-06-20
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/expert/customerSolutionAndProduct")
public class CustomerSolutionAndProductController extends BaseController {

	@Autowired
	private CustomerSolutionAndProductService customerSolutionAndProductService;
	@Autowired
	private CustomerSolutionService customerSolutionService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private StoreProductService storeProductService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private StoreProductDao storeProductDao;
	
	@ModelAttribute
	public CustomerSolutionAndProduct get(@RequestParam(required=false) Long cid, 
			@RequestParam(required=false) Long pid) {
		if (cid != null && pid != null){
			return customerSolutionAndProductService.getOneByPK(cid, pid);
		}else{
			return new CustomerSolutionAndProduct();
		}
	}
	
	@RequestMapping(value = "detail")
	public String list(CustomerSolutionAndProduct customerSolutionAndProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		String csId = request.getParameter("customerSolutionId");
		String target = request.getParameter("target");
		Long customerSolutionId = Long.parseLong(csId);
		CacheUtils.put("customerSolution_id", customerSolutionId);
		CustomerSolution customerSolution = customerSolutionService.get(customerSolutionId);
		CustomerExam customerExam = customerSolution.getCustomerExam();
		Long customerExamId = customerExam.getId();
		if (customerSolutionId != null) {
			List<CustomerSolutionAndProduct> list = customerSolutionAndProductService.find(customerSolutionId); 
			model.addAttribute("customerSolutionId", csId);
			model.addAttribute("list", list);
		}
		model.addAttribute("target", target);
		model.addAttribute("customerExamId", customerExamId);
		model.addAttribute("customerId", customerExam.getCustomer().getId());
		return "modules/expert/customerSolutionAndProductDetail";
	}
	
	@RequestMapping(value = "logForm")
	public String detailForm(CustomerSolutionAndProduct customerSolutionAndProduct, HttpServletRequest request, Model model) {
		model.addAttribute("customerSolutionAndProduct", customerSolutionAndProduct);
		return "modules/expert/customerSolutionAndProductLog";
	}
	
	@ResponseBody
	@RequestMapping(value = "checkNum")
	public String checkNum(HttpServletRequest request, Model model) {
		String pId = request.getParameter("pId");
		StoreProduct spProduct = storeProductDao.findOne(Long.parseLong(pId));
		int currentAmount= spProduct.getAmount();
		return "{\"lastNum\": "+currentAmount +"}";
		
	}
	@ResponseBody
	@RequestMapping(value = "add")
	public String detailForm(HttpServletRequest request, Model model) {
		String csId = request.getParameter("csId"); //方案ID
		System.out.println("----" +csId);
		String pId = request.getParameter("pId");
		String num = request.getParameter("num");
		String employeeId = request.getParameter("employeeId");
		String customerId = request.getParameter("customerId");
		Member member = StoreUtils.getMember();
		if (member == null) {
			member = new Member();
			member.setCustomer(customerService.get(Long.parseLong(customerId)));
			request.getSession().setAttribute("member", member);
		}
		memberService.loadingMemberInfo(member);
		MemberShoppingCart shop = member.getShoppingCart();
		// 购物车
		SolutionStoreProductCart storeProductCart = new SolutionStoreProductCart();
		storeProductCart.setAppend(true);
		storeProductCart.setSolutionID(Long.parseLong(csId));
		storeProductCart.setStoreProduct(storeProductService.get(Long.parseLong(pId)));
		storeProductCart.setEmployee(employeeService.get(Long.parseLong(employeeId)));
		storeProductCart.setCount(Integer.parseInt(num));
		shop.addSolutionStoreProduct(storeProductCart);
		return "{'result':true}";
	}
}
